require 'View/UIBase/UIBase'

UIRuneInfo= UIBase.New(PanelNames.UIRuneInfo)

local specialRuneIndex = 6  --天穹符文index

function UIRuneInfo.Init(runeInfo, isBag)
    UIRuneInfo.runeInfo = runeInfo
    UIRuneInfo.isBag = isBag
    UIBase.Init(UIRuneInfo)
end

function UIRuneInfo:OnCreate()
    self.btnClose = self:GetChild("bgclose")
    self.btnLevelUp = self:GetChild("btnLevelUp")
    self.btnBaptism = self:GetChild("btnBaptism")
    self.btnPutOff = self:GetChild("btnRemove")
    self.btnChange = self:GetChild("btnChange")
    self.btnLock = self:GetChild("btnLock")
    self.comMoney = self:GetChild("currency")
    self.item = self:GetChild("item")
    self.txtName = self:GetChild("name")
    self.imgType = self:GetChild("type")
    self.txtType = self:GetChild("textType")
    self.txtPower = self:GetChild("power")
    self.ctrlType = self:GetController("type")
    self.ctrlSuit = self:GetController("suit")
    self.comSkillInfo = self:GetChild("debrisInfo")
    self.listAttr = self.comSkillInfo:GetChild("attrlist")
    self.listBaptism = self.comSkillInfo:GetChild("list_baptism")
    self.txtSkillName = self.comSkillInfo:GetChild("SkillName")
    self.txtSkillDesc = self.comSkillInfo:GetChild("SkillDes")
    self.imgSkill = self.comSkillInfo:GetChild("n21"):GetChild("Mask"):GetChild("Icon")
    self.ctrlShowSkill = self.comSkillInfo:GetController("subSkill")
    self.attributeShowConfig = dataTable.getDataTable("t_attributelibrary")
    self.nowBaseAttrList = {}
    self.oriBaseAttrList = {}
    self.showMoneyId = 1
    self:AddEvent()
    self:InitView()
    self:Refresh()
end

function UIRuneInfo:AddEvent()
    self:RegisterOnClick(self.btnClose,self.OnClickClose)
    self:RegisterOnClick(self.btnLevelUp,self.OnClickLevelUp)
    self:RegisterOnClick(self.btnBaptism,self.OnClickBaptism)
    self:RegisterOnClick(self.btnPutOff,self.OnClickPutOff)
    self:RegisterOnClick(self.btnChange,self.OnClickChange)
    --self:RegisterOnClick(self.btnLock, self.OnClickLock)
    self:SetDynamicList(self.listAttr ,self.AttrItemRender)
    self:SetDynamicList(self.listBaptism ,self.BaptismItemRender)
    self:RegisterEvent(Config.EventType.FreshHeroRuneLock,self.Refresh)
end

function UIRuneInfo:InitView()
    if self.isBag then
        self.runeInfo = logicMgr.RuneManager.FormationRuneInfo(self.runeInfo,self.runeInfo.partnerId)
    end
    if self.runeInfo then
        self.txtName.text = self.runeInfo.name
        --self.imgType = self:GetChild("type")
        --self.txtType = self:GetChild("textType")
        self.ctrlSuit.selectedIndex = self.runeInfo.type
        self.txtPower.text = HelperFunc.FormatResourceNum(self.runeInfo.power, true)
        logicMgr.RuneManager.SetRuneItemData(self.item,self.runeInfo)
        self.item.onClick:Clear()
        self.nowBaseAttrList = self.runeInfo.baseAttr
        local oriRuneData = logicMgr.RuneManager.GetRuneCfgInfo(self.runeInfo.itemModelId)
        self.oriBaseAttrList = oriRuneData.f_BaseAttr
        self.listAttr.numItems = #self.nowBaseAttrList
        self.baptismList = self.runeInfo.baptismAttr
        if self.baptismList and #self.baptismList > 0 then
            self.listBaptism.numItems = #self.baptismList
        else
            local runeOriginCfg = logicMgr.RuneManager.GetRuneCfgInfo(tostring(self.runeInfo.itemModelId))
            self.listBaptism.numItems = tonumber(runeOriginCfg.f_RuneRefineNum)
        end
        local need = logicMgr.RuneManager.GetRuneLevelUpCost(self.runeInfo.itemModelId, self.runeInfo.level)
        if need and need > 0 then
            self.btnLevelUp.grayed = false
        else
            self.btnLevelUp.grayed = true
        end
        if self.runeInfo.partnerId then
            self.ctrlType.selectedIndex = 0
            UIRedDots.addViewListen(PanelNames.UIRuneInfo, self.fguiObj.name .. self.btnLevelUp.name, self.btnLevelUp, Config.RedDotsType.RuneLevelUp, self.runeInfo.partnerId, self.runeInfo.index)
            UIRedDots.addViewListen(PanelNames.UIRuneInfo, self.fguiObj.name .. self.btnChange.name, self.btnChange, Config.RedDotsType.RuneChange, self.runeInfo.partnerId, self.runeInfo.index)
        else
            self.ctrlType.selectedIndex = 1
        end
        if self.runeInfo.commonType == 2 then
            local suitCfg = dataTable.getDataTableByStringId("t_runesuit",self.runeInfo.itemModelId)
            local skillInfo = dataTable.getDataTableByStringId("t_skillshow", suitCfg.f_Effect)
            if skillInfo then
                self.imgSkill.url = "ui://SkillAtlas/" .. skillInfo.f_SkillIcon
            end
            self.txtSkillName.text = getLanguage(suitCfg.f_RuneSuitName)
            self.txtSkillDesc.text = getLanguage(suitCfg.f_EffectDesc)
            self.ctrlShowSkill.selectedIndex = 1
        else
            self.ctrlShowSkill.selectedIndex = 0
        end
    end
end

function UIRuneInfo:Refresh()
    --self.btnLock.selected = self.runeInfo.lock == 1
end

function UIRuneInfo:BaptismItemRender(index, go)
    local data = self.baptismList[index]
    local txtName = go:GetChild("title")
    local txtNum = go:GetChild("preNum")
    local ctrQuality = go:GetController("Quality")
    local ctrlType = go:GetController("type")
    ctrlType.selectedIndex = 1
    if data then
        ctrQuality.selectedIndex = 0
        local at = G.dataTable["t_attributelibrary"][tostring(data[1])]
        if at then
            txtName.text = at.f_AttributeName
            local type = at.f_AttributeType
            if type == 2 then
                txtNum.text =  data[2]*0.01 .. "%"
            else
                txtNum.text = data[2]
            end
        end
    else
        ctrQuality.selectedIndex = 2
    end
end

function UIRuneInfo:AttrItemRender(index, go)
    local txt_name = go:GetChild("name")
    local txt_value = go:GetChild("refine")
    local txt_refine = go:GetChild("value")
    local nowAttr = self.nowBaseAttrList[index]
    local oriAttr = self.oriBaseAttrList[index]
    local at = G.dataTable["t_attributelibrary"][tostring(nowAttr[1])]
    if at and oriAttr then
        local basic = oriAttr[2] or 0
        txt_name.text = at.f_AttributeName
        txt_value.text = basic
        local delta = nowAttr[2] - basic
        if delta == 0 then
            txt_refine.text = ""
        else
            local type = at.f_AttributeType
            if type == 2 then
                txt_refine.text =  "+" .. delta * 0.01 .. "%"
            else
                txt_refine.text =  "+" .. delta
            end
        end
    end
end

function UIRuneInfo:OnClickLevelUp()
    showUI(PanelNames.UICharactorInfoHeroRuneRefine, self.runeInfo)
    self:OnClickClose()
end

function UIRuneInfo:OnClickBaptism()
    showUI(PanelNames.UIRuneBaptism, self.runeInfo.partnerId, self.runeInfo.index)
    self:OnClickClose()
end

function UIRuneInfo:OnClickPutOff()
    if self.runeInfo.partnerId and self.runeInfo.index then
        runeHandler.reqUnInlayRune(self.runeInfo.partnerId, self.runeInfo.itemId, self.runeInfo.index)
        self:OnClickClose()
    end
end

function UIRuneInfo:OnClickChange()
    if self.runeInfo.partnerId and self.runeInfo.index then
        showUI(PanelNames.UIRuneList, self.runeInfo.partnerId, self.runeInfo.index)
        self:OnClickClose()
    end
end

function UIRuneInfo:OnClickLock()
    --local lock = self.runeInfo.lock == 1 and 0 or 1
    --runeHandler.reqRuneLock(self.runeInfo.partnerId, self.runeInfo.itemId, self.runeInfo.index, lock)
end

function UIRuneInfo:OnClickClose()
    UIRedDots.removeViewEvent(PanelNames.UIRuneInfo)
    closeUI(PanelNames.UIRuneInfo)
end

return UIRuneInfo